package leetcode.array;

/**
 * 移除元素
 *
 * @author WangChenyu
 * @since 2024-04-16 17:31
 */
public class RemoveElement {

    public static int removeElement(int[] nums, int val) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == val) {
                nums[i] = -1;
                k++;
            }
        }

        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != -1) {
                int p = nums[i];
                nums[i] = -1;
                nums[j++] = p;
            }
        }
        return nums.length - k;
    }

    public static void main(String[] args) {
        int[] nums = {3,2,2,3};
        System.out.println(removeElement(nums, 3));
    }
}
